package com.expert.conrtoller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.expert.annotations.RoleAnnotation;
import com.expert.enums.RoleLevel;
import com.expert.pojo.Category;
import com.expert.pojo.Expert;
import com.expert.pojo.User;
import com.expert.result.Result;
import com.expert.service.CategoryService;
import com.expert.service.ExpertService;
import com.expert.vo.ExpertInfoVo;
import com.expert.vo.ExpertPageVo;
import com.expert.vo.RemoteExpertVo;
import com.expert.vo.RemoteUserVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ExpertController
 * @Author: cxs
 * @Date: 2024/03/14 13:06
 * @Description:
 **/
@RestController
@RequestMapping("/expert")
@Slf4j
public class ExpertController {

    @Autowired
    ExpertService expertService;
    @Autowired
    CategoryService categoryService;

    /**
     * 添加专家信息
     * @param expert 前端填写的专家信息
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PostMapping("/addExpert")
    public Result addExpert(@RequestBody Expert expert){

      log.info("添加专家信息：{}",expert);
      this.expertService.addExpertService(expert);
      return Result.success();
    }

    /**
     * 分页查询专家信息
     * @param page
     * @param pageSize
     * @param expertName
     * @param expertSex
     * @param expertTitle
     * @param expertEducation
     * @param expertUnit
     * @param expertCategory
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getExpertPage")
    public  Result<Page<ExpertPageVo>> getExpertPage(Integer page,Integer pageSize,
                                                     String expertName, Integer expertSex,
                                                     String expertTitle, String expertEducation,
                                                     String expertUnit, Long expertCategory){
        log.info("分页查询专家信息");
        Page<ExpertPageVo> expertPage=this.expertService.pageExpertService(page,pageSize,expertName,expertSex,expertTitle,expertEducation,expertUnit,expertCategory);
        return Result.success(expertPage);
    }


    /**
     * 根据id获取专家信息
     * @param id 专家id
     * * @return
     */
    @GetMapping("/getExpertInfoById/{id}")
    public Result<ExpertInfoVo> getExpertInfoById(@PathVariable("id")Long id ){
        log.info("根据id获取专家信息");
        ExpertInfoVo expertInfoVo=this.expertService.getInfoById(id);
        return Result.success(expertInfoVo);
    }

    /**
     * 删除专家信息
     * @param ids
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @DeleteMapping("/deleteExperts")
    public  Result deleteExpert(@RequestParam List<Long> ids){
        log.info("删除专家信息");
        this.expertService.deleteExpert(ids);
        return Result.success();
    }

    /**
     * 修改专家信息
     * @param expert
     * @return
     */
    @RoleAnnotation(level = RoleLevel.USER)
    @PutMapping("/updateExpert")
    public Result deleteExpert(@RequestBody Expert expert){
        log.info("修改专家信息");
        this.expertService.updateExpertService(expert);
        return Result.success();
    }
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/selectByName")
    public Result<List<RemoteExpertVo>> selectByName(@RequestParam String name){
        log.info("根据名字查询专家");
        List<RemoteExpertVo> list= this.expertService.selectByName(name);
        return  Result.success(list);
    }
    @GetMapping("/selectByUserId/{id}")
    public Result<ExpertInfoVo> selectByUserId(@PathVariable("id") Long id){
        Expert expert = this.expertService.lambdaQuery().eq(Expert::getExpertUser, id).one();
        if (expert==null){
            expert=new Expert();
        }

        expert.setExpertIntroduce(null);
        ExpertInfoVo expertInfoVo=new ExpertInfoVo();
        BeanUtils.copyProperties(expert,expertInfoVo);
        if (expert.getExpertCategory()!=null) {
            Category category = this.categoryService.selectById(expert.getExpertCategory());
            expertInfoVo.setExpertCategoryName(category.getCategoryCode() + " " + category.getCategoryName());
        }
        return Result.success(expertInfoVo);
    }
}
